package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.Forum;
import com.example.pojo.PageBean;
import com.example.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 二七和夭夭
 * @email 2646498606@qq.com
 * @date 2022/8/24 10:44
 */
@Controller
@RequestMapping("/forumManage")
public class ForumManageController {
    @Autowired
    private ForumService forumService;

    /**
     * 管理页面展示
     * @param model
     * @return
     */
    @RequestMapping("/list.do")
    public String getList(Model model,Integer currentPage,Integer pageSize) {
        if (currentPage==null) currentPage = 1;
        if (pageSize==null) pageSize = 2;
        //查询forumList
        PageBean<Forum> pageBean = forumService.getPage(currentPage,pageSize);
        model.addAttribute("forumList", pageBean.getRecordList());
        model.addAttribute("pageBean", pageBean);

        model.addAttribute("url", "/forumManage/list.do?");

        return "/jsp/forumManage/list.jsp";
    }

    /**
     * 前往添加论坛
     * @return
     */
    @RequestMapping("/addUI.do")
    public String toAddUI() {
        return "/jsp/forumManage/save.jsp";
    }

    /**
     * 添加模块(默认放在最后)
     * @param name
     * @param description
     * @return
     */
    @RequestMapping("/add.do")
    public String addForum( String name, String description, HttpServletRequest req) {
        //首先获取排序的最后一个
        LambdaQueryWrapper<Forum> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Forum::getPosition);
        List<Forum> forumList = forumService.list(wrapper);
        Forum lastForum = forumList.get(0);

        //主题对象
        Forum forum = new Forum();
        forum.setName(name);
        forum.setDescription(description);
        forum.setPosition(lastForum.getPosition()+1);//排序(默认放在最后一个)

        //添加
        forumService.save(forum);

        return "redirect:/forumManage/list.do";
    }

    /**
     * 删除论坛
     * @param fid
     * @return
     */
    @RequestMapping("/delete.do")
    public String deleteForum(Integer fid) {
        boolean delete = forumService.deleteForumWithSon(fid);
        return "redirect:/forumManage/list.do";
    }

    /**
     * 前往修改页面
     * @return
     */
    @RequestMapping("/editUI.do")
    public String toEditUI(Model model,Integer fid) {
        //查询forum
        List<Forum> forumList = forumService.getList(fid);
        Forum forum = forumList.get(0);

        model.addAttribute("forum", forum);
        return "/jsp/forumManage/save.jsp";
    }

    /**
     * 修改论坛
     * @return
     */
    @RequestMapping("/edit.do")
    public String editForum(Integer fid,String name,String description) {
        //修改的对象
        Forum forum = new Forum();
        forum.setFid(fid);
        forum.setName(name);
        forum.setDescription(description);

        forumService.updateById(forum);
        return "redirect:/forumManage/list.do";
    }

    /**
     * 论坛上移
     * @param fid
     * @return
     */
    @RequestMapping("/moveUp.do")
    public String moveUp(Integer fid,Integer currentPage,Integer pageSize) {
        boolean moveUp = forumService.moveUp(fid);
        return "redirect:/forumManage/list.do?currentPage="+currentPage+"&pageSize="+pageSize;
    }

    /**
     * 论坛下移
     * @param fid
     * @return
     */
    @RequestMapping("/moveDown.do")
    public String moveDown(Integer fid,Integer currentPage,Integer pageSize) {
        boolean moveDown = forumService.moveDown(fid);
        return "redirect:/forumManage/list.do?currentPage="+currentPage+"&pageSize="+pageSize;
    }
}
